package org.budo.netty.servlet.server;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletContext;

import org.budo.netty.servlet.server.connector.Connector;
import org.budo.netty.servlet.server.event.BudoEventBus;
import org.budo.netty.servlet.server.event.EventBus;
import org.budo.netty.servlet.server.exception.handler.ExceptionHandler;
import org.budo.netty.servlet.server.handler.servlet.BudoServletContext;
import org.budo.netty.servlet.server.handler.servlet.DefaultBudoServletContext;
import org.budo.netty.servlet.server.resource.loader.ResourceLoader;
import org.budo.netty.servlet.server.session.repository.SessionRepository;
import org.budo.netty.servlet.server.session.repository.SessionRepositoryWrapper;

/**
 * @author li
 */
public abstract class AbstractBudoServletServer implements BudoServletServer {
    private List<String> welcomeFileList = new ArrayList<String>();

    private ServletContext servletContext;

    private ResourceLoader resourceLoader;

    private SessionRepository sessionRepository;

    private ExceptionHandler exceptionHandler;

    private Connector connector;

    private EventBus eventBus = new BudoEventBus();

    public void setEventBus(EventBus eventBus) {
        this.eventBus = eventBus;
    }

    @Override
    public EventBus getEventBus() {
        return this.eventBus;
    }

    @Override
    public Connector getConnector() {
        return this.connector;
    }

    public void setConnector(Connector connector) {
        this.connector = connector;
    }

    public void setExceptionHandler(ExceptionHandler exceptionHandler) {
        this.exceptionHandler = exceptionHandler;
    }

    public ExceptionHandler getExceptionHandler() {
        return this.exceptionHandler;
    }

    public List<String> getWelcomeFileList() {
        return this.welcomeFileList;
    }

    public ServletContext getServletContext() {
        if (null != servletContext) {
            return this.servletContext;
        }

        synchronized (this) {
            BudoServletContext budoServletContext = new DefaultBudoServletContext(this);
            return this.servletContext = budoServletContext;
        }
    }

    public void setSessionRepository(SessionRepository sessionRepository) {
        this.sessionRepository = new SessionRepositoryWrapper(sessionRepository);
    }

    public SessionRepository getSessionRepository() {
        return this.sessionRepository;
    }

    public void setServletContext(ServletContext servletContext) {
        this.servletContext = servletContext;
    }

    public ResourceLoader getResourceLoader() {
        return this.resourceLoader;
    }

    public void setResourceLoader(ResourceLoader resourceLoader) {
        this.resourceLoader = resourceLoader;
    }
}